Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices

Namespace C4F.DevKit.Messaging
    ''' <summary>
    ''' Imports functionalities from native API.
    ''' </summary>
    Friend Class MapiManagementNative
#Region "DllImports"

        <DllImport("mapi32.dll")> _
        Friend Shared Function MAPIInitialize(ByVal lpMapiInit As IntPtr) As Int32
        End Function
        <DllImport("mapi32.dll")> _
        Friend Shared Function MAPIAdminProfiles(ByVal i As Int32, ByRef a As IProfAdmin) As Int32
        End Function
        <DllImport("MAPI32.DLL", CharSet:=CharSet.Ansi)> _
        Friend Shared Function MAPILogon(ByVal handle As IntPtr, ByVal profileName As String, ByVal password As String, ByVal flag As Integer, ByVal reserved As Integer, ByRef session As IntPtr) As Integer
        End Function
        <DllImport("MAPI32.DLL")> _
        Friend Shared Function MAPILogoff(ByVal session As IntPtr, ByVal handle As IntPtr, ByVal flag As Integer, ByVal reserved As Integer) As Integer
        End Function
        <DllImport("MAPI32.DLL")> _
        Friend Shared Function MAPISendMail(ByVal session As IntPtr, ByVal handle As IntPtr, ByVal message As MapiMessage, ByVal flag As Integer, ByVal reserved As Integer) As Integer
        End Function
        <DllImport("MAPI32.DLL", CharSet:=CharSet.Ansi)> _
        Friend Shared Function MAPIFindNext(ByVal session As IntPtr, ByVal handle As IntPtr, ByVal typ As String, ByVal seed As String, ByVal flag As Integer, ByVal reserved As Integer, _
         ByVal id As StringBuilder) As Integer
        End Function
        <DllImport("MAPI32.DLL", CharSet:=CharSet.Ansi)> _
        Friend Shared Function MAPIReadMail(ByVal session As IntPtr, ByVal handle As IntPtr, ByVal id As String, ByVal flag As Integer, ByVal reserved As Integer, ByRef ptrmsg As IntPtr) As Integer
        End Function
        <DllImport("MAPI32.DLL")> _
        Friend Shared Function MAPIFreeBuffer(ByVal ptr As IntPtr) As Integer
        End Function
        <DllImport("MAPI32.DLL", CharSet:=CharSet.Ansi)> _
        Friend Shared Function MAPIDeleteMail(ByVal session As IntPtr, ByVal handle As IntPtr, ByVal id As String, ByVal flag As Integer, ByVal reserved As Integer) As Integer
        End Function

#End Region

#Region "InterfaceImports"

        ''' <summary>
        ''' .Net wrapper over IMAPITable interface.
        ''' </summary>
        <Guid("00020301-0000-0000-c000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
        Public Interface IMAPITable

            <PreserveSig()> _
            Function GetLastError(<[In]()> _
            <MarshalAs(UnmanagedType.[Error])> _
            ByVal hResult As Int32, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32, <Out()> _
            <MarshalAs(UnmanagedType.LPStruct)> _
            ByVal lppMAPIError As IntPtr) As Int32

            <PreserveSig()> _
            Function Advise(<[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulEventMask As UInt32, ByVal lpAdviseSink As IntPtr, ByVal lpulConnection As IntPtr) As Int32

            <PreserveSig()> _
            Function Advise(<[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulConnection As UInt32) As Int32

            <PreserveSig()> _
            Function GetStatus(ByVal lpulTableStatus As IntPtr, ByVal lpulTableType As IntPtr) As Int32

            <PreserveSig()> _
            Function SetColumns(ByRef lpPropTagArray As IntPtr, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32) As Int32

            <PreserveSig()> _
            Function QueryColumns(<[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32, ByVal lpPropTagArray As IntPtr) As Int32

            '<[return]: MarshalAs(UnmanagedType.[Error])> 
            <PreserveSig()> _
            Function GetRowCount(<[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32, ByRef lpulCount As UInt32) As Int32

            <PreserveSig()> _
            Function SeekRow(ByVal bkOrigin As IntPtr, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal lRowCount As UInt32, ByVal lplRowsSought As IntPtr) As Int32

            <PreserveSig()> _
            Function SeekRowApprox(<[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulNumerator As UInt32, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulDenominator As UInt32) As Int32

            <PreserveSig()> _
            Function QueryPosition(ByVal lpulRow As IntPtr, ByVal lpulNumerator As IntPtr, ByVal lpulDenominator As IntPtr) As Int32

            <PreserveSig()> _
            Function FindRow(ByRef lpRestriction As IntPtr, ByVal BkOrigin As UInt32, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32) As Int32

            <PreserveSig()> _
            Function Restrict(ByRef lpRestriction As IntPtr, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32) As Int32

            <PreserveSig()> _
            Function CreateBookmark(ByVal lpbkPosition As IntPtr) As Int32

            <PreserveSig()> _
            Function FreeBookmark(ByVal bkPosition As IntPtr) As Int32

            <PreserveSig()> _
            Function SortTable(ByVal lpSortCriteria As IntPtr, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32) As Int32

            <PreserveSig()> _
            Function QuerySortOrder(ByVal lppSortCriteria As IntPtr) As Int32

            <PreserveSig()> _
            Function QueryRows(ByVal lRowCount As UInt32, ByVal ulFlags As UInt32, ByRef lppRows As IntPtr) As Int32

            <PreserveSig()> _
            Function Abort() As Int32

            <PreserveSig()> _
            Function ExpandRow(<[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal cbInstanceKey As UInt32, ByVal pbInstanceKey As IntPtr, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulRowCount As UInt32, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32, ByVal lppRows As IntPtr, ByVal lpulMoreRows As IntPtr) As Int32

            <PreserveSig()> _
            Function CollapseRow(<[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal cbInstanceKey As UInt32, ByVal pbInstanceKey As IntPtr, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32, ByVal lpulRowCount As IntPtr) As Int32

            <PreserveSig()> _
            Function WaitForCompletion(<[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulTimeout As UInt32, ByVal lpulTableStatus As IntPtr) As Int32

            <PreserveSig()> _
            Function GetCollapseState(<[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal cbInstanceKey As UInt32, ByVal lpbInstanceKey As IntPtr, ByVal lpcbCollapseState As IntPtr, ByVal lppbCollapseState As IntPtr) As Int32

            <PreserveSig()> _
            Function SetCollapseState(<[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal cbCollapseState As UInt32, ByVal pbCollapseState As IntPtr, ByVal lpbkLocation As IntPtr) As Int32

        End Interface

        ''' <summary>
        ''' .Net wrapper over IProfAdmin interface.
        ''' </summary>
        <Guid("0002031c-0000-0000-c000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
        Public Interface IProfAdmin

            <PreserveSig()> _
            Function GetLastError(<[In]()> _
            <MarshalAs(UnmanagedType.[Error])> _
            ByVal hResult As Int32, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32, <Out()> _
            <MarshalAs(UnmanagedType.LPStruct)> _
            ByVal lppMAPIError As IntPtr) As Int32

            <PreserveSig()> _
            Function GetProfileTable(<[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32, <Out()> _
            ByRef lppTable As IMAPITable) As Int32

            <PreserveSig()> _
            Function CreateProfile(ByVal lpszProfileName As IntPtr, ByVal lpszPassword As IntPtr, ByVal ulUIParam As IntPtr, ByVal ulFlags As UInt32) As Int32

            <PreserveSig()> _
            Function DeleteProfile(ByVal lpszProfileName As IntPtr, ByVal ulFlags As UInt32) As Int32

            <PreserveSig()> _
            Function ChangeProfilePassword(<[In]()> _
            <MarshalAs(UnmanagedType.LPWStr)> _
            ByVal lpszProfileName As String, <[In]()> _
            <MarshalAs(UnmanagedType.LPWStr)> _
            ByVal lpszOldPassword As String, <[In]()> _
            <MarshalAs(UnmanagedType.LPWStr)> _
            ByVal lpszNewPassword As String, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32) As Int32

            <PreserveSig()> _
            Function CopyProfile(ByVal lpszOldProfileName As IntPtr, ByVal lpszOldPassword As IntPtr, ByVal lpszNewProfileName As IntPtr, ByVal ulUIParam As IntPtr, ByVal ulFlags As IntPtr) As Int32

            <PreserveSig()> _
            Function RenameProfile(ByVal lpszOldProfileName As IntPtr, ByVal lpszOldPassword As IntPtr, ByVal lpszNewProfileName As IntPtr, ByVal ulUIParam As IntPtr, ByVal ulFlags As IntPtr) As Int32

            <PreserveSig()> _
            Function SetDefaultProfile(ByVal lpszProfileName As IntPtr, ByVal ulFlags As UInt32) As Int32

            <PreserveSig()> _
            Function AdminServices(<[In]()> _
            <MarshalAs(UnmanagedType.LPWStr)> _
            ByVal lpszProfileName As String, <[In]()> _
            <MarshalAs(UnmanagedType.LPWStr)> _
            ByVal lpszPassword As String, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulUIParam As UInt32, <[In]()> _
            <MarshalAs(UnmanagedType.U4)> _
            ByVal ulFlags As UInt32, <Out()> _
            <MarshalAs(UnmanagedType.IUnknown)> _
            ByVal lppServiceAdmin As IntPtr) As Int32
        End Interface
#End Region

#Region "Classes and Structures"

#Region "Message Classes"

        ''' <summary>
        ''' .Net wrapper over MapiMessage native structure.
        ''' </summary>
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
        Friend Class MapiMessage
            Public reserved As Integer
            Public subject As String
            Public messageBody As String
            'IPM or non- IPM Interpersonal Message.
            Public messageType As String
            Public dateReceived As String
            Public conversationID As String
            Public flags As Integer
            Public originator As IntPtr
            Public recipientCount As Integer
            Public recipients As IntPtr
            Public fileCount As Integer
            Public files As IntPtr
        End Class

        ''' <summary>
        ''' .Net wrapper over MapiRecipDesc native structure.
        ''' </summary>
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
        Friend Class MapiRecipDesc
            Public reserved As Integer
            Public recipientClass As Integer
            Public name As String
            Public address As String
            Public eIDSize As Integer
            Public entryID As IntPtr
        End Class

        ''' <summary>
        ''' .Net wrapper over MapiFileDesc native structure.
        ''' </summary>
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
        Friend Class MapiFileDesc
            Public reserved As Integer
            Public flags As Integer
            Public position As Integer
            Public pathName As String
            Public fileName As String
            Public fileType As IntPtr
        End Class

#End Region

#Region "Profile Structures"

        ''' <summary>
        ''' .Net wrapper over SRowSet native structure.
        ''' </summary>
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _
        Friend Structure RowSet
            ' Count of rows.
            Public countRows As UInt32

            ' Array of rows.
            <MarshalAs(UnmanagedType.ByValArray, SizeConst:=1)> _
            Public row As Row()

        End Structure

        ''' <summary>
        ''' .Net wrapper over SRow native structure.
        ''' </summary>
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
        Friend Structure Row
            ' Pad so SRow's can map to ADRENTRY's.
            Public ulAdrEntryPad As UInt32

            ' Count of property values.
            Public cValues As UInt32

            'Property value array.
            Public lpProps As PropValue

        End Structure

        ''' <summary>
        ''' .Net wrapper over SPropValue native structure.
        ''' </summary>
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
        Friend Structure PropValue
            Public ulPropTag As UInt32
            Public dwAlignPad As UInt32
            Public Value As PropertyValue
        End Structure

        ''' <summary>
        ''' .Net wrapper over SPropertyValue native union.
        ''' </summary>
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
        Friend Structure PropertyValue
            Public i As Int16
            Public l As Int32
            Public ul As UInt32
            Public flt As Single
            Public dbl As Double
            Public b As UInt16
            Public cur As Single
            Public at As Double
            Public ft As System.Runtime.InteropServices.ComTypes.FILETIME
            Public lpszA As String
            Public bin As IntPtr
            Public lpszW As String

#Region "Commented because of known issue of GetAvailableProfiles"
            'public IntPtr lpguid;		/* case PT_CLSID */
            'public Int64 li;			/* case PT_I8 */
            'public IntPtr MVi;		/* case PT_MV_I2 */
            'public IntPtr MVl;		/* case PT_MV_LONG */
            'public IntPtr MVflt;		/* case PT_MV_R4 */
            'public IntPtr MVdbl;		/* case PT_MV_DOUBLE */
            'public IntPtr MVcur;		/* case PT_MV_CURRENCY */
            'public IntPtr MVat;		/* case PT_MV_APPTIME */
            'public IntPtr MVft;		/* case PT_MV_SYSTIME */
            'public IntPtr MVbin;		/* case PT_MV_BINARY */
            'public IntPtr MVszA;		/* case PT_MV_STRING8 */
            'public IntPtr MVszW;		/* case PT_MV_UNICODE */
            'public IntPtr MVguid;		/* case PT_MV_CLSID */
            'public IntPtr MVli;		/* case PT_MV_I8 */
            'public IntPtr err;		/* case PT_ERROR */
            ' public Int32 x;			/* case PT_NULL, PT_OBJECT (no usable value) */
#End Region
        End Structure

#End Region

#End Region
    End Class
End Namespace
